/**
 * @Description  IPAddressUtil.java
 * @author xu_cc
 * @date 创建时间：2017年5月25日 下午4:13:05
 */
package com.solution.common.utils.ip;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取IP通用方法
 * 
 * @author xu_cc
 * @date 创建时间：2017年11月3日 下午3:21:09
 */
public class IPUtils {

    /**
     * 根据request获取客户端公网IP（header里关于key的大小写很有影响）<br>
     * 如：X-Forwarded-For 和 x-forwarded-for
     * 
     * @param request
     * @return String
     * @author xu_cc
     * @throws Exception
     *             exception
     * @Date 创建时间：2017年11月3日 下午3:21:16
     */
    public static String getIntenetIP(HttpServletRequest request) {
        try {
            String ip = request.getHeader("remoteip");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "null".equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Forwarded-For");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "null".equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Real-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "null".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "null".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || "null".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return ip;
        } catch (Exception e) {
            e.printStackTrace();
            return request.getRemoteAddr();
        }
    }

}
